import axios from "axios";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

const request = axios.create({
  baseURL: "/api",
  timeout: 20000,

});
request.interceptors.request.use(
  // 成功的回调函数
  (config) => {
    // config请求的配置对象
    NProgress.start();
    return config;
  }
);

request.interceptors.response.use(
  (response) => {
    NProgress.done();
    if (response.data.code === 200) {
      // 功能成功
      return response.data.data;
    } else {
      // 功能失败
      return Promise.reject(response.data.message || "未知错误，请联系网管");
    }
  },
  // 失败
  (error) => {
    NProgress.done();
    console.dir(error);
    // 请求失败
    return Promise.reject(error.message);
  }
);

export default request;